﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Binary search</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <div id="js-console"></div>
	<script src="scripts/js-console.js"></script>

    <script>
        var array = [ 3, 4, 5, 6, 9, 10, 20, 30, -2, 4, 8 ];
           
        for (var i = 0; i < array.length; i++){
            
            for (var j = i; j < array.length; j++){
                
                    if (array[j] < array[i]){
                        
                        var buf = array[i];
                        array[i] = array[j];
                        array[j] = buf;
                    }
                }
        }

        for (var i = 0; i < array.length; i++)
        {
            jsConsole.write(array[i] + "  ");
        }

        var min = 0;
        var max = array.length - 1;
        var mid;
        var x = 30;
        do {
        
            mid = (min + max) / 2;
            if (x > array[mid])
            {
                min = mid + 1;
            }
            else
            {
                max = mid - 1;
            }
        }
        while (array[mid] != x && (min <= max));
    </script>
</body>
</html>
